$(function(){

	//轮播  

	$('#pause').on('click', function(){
		$('#pause').css('display','none');
		$('#play').css('display','inline-block');
		$("#myCarousel").carousel('pause');
	});
	$('#play').on('click',function(){
		$('#pause').css('display','inline-block');
		$('#play').css('display','none');
		$("#myCarousel").carousel('cycle');
		//$('#myCarousel').carousel({interval: 2000});
	});
	
	//link搜索效果
	var refresh = function () {
		setTimeout(function(){
			if($('#showsearch').val() === '') {
				$('#deletesearch').css('display','none');
				$('#sitesPeople').css('display','none');
				$('#tips').css('display','none');
				$('#myLinksDivPrimary').css('display','block');
				$('#tipscontent').css('display','block');
			}else {
				$('#deletesearch').css('display','inline-block');
				$('#hiddenPanel').css('display','block');
				$('#sitesPeople').css('display','block');
				$('#tips').css('display','block');
			}
		},500);
	}
	$('#showsearch').on('click', function () {
		if($('#showsearch').val() === '') {
			$('#hiddenPanel').css('display','block');
			$('#myLinksDivPrimary').css('display','block');
			$('#tipscontent').css('display','block');		
		}else {
			$('#deletesearch').css('display','inline-block');
			$('#hiddenPanel').css('display','block');
			$('#sitesPeople').css('display','block');
			$('#tips').css('display','block');
		}
	});

	/*$('#showsearch').on('blur', function () {
		clearTimeout(refresh);
		$('#hiddenPanel').css('display','none');
		$('#deletesearch').css('display','none');
	});*/
	//----------------------------------------------------------------
	//-------------------------------------------------------------
	
	/*$("#searchAreaBlock1").focusout(function(){
		alert("hahahahh")
//		clearTimeout(refresh);
//		$('#hiddenPanel').css('display','none');
//		$('#deletesearch').css('display','none');
	})*/
	
	$(function () {
		$(document).bind("click",function(e){
			var target = $(e.target);
			if(target.closest(".search-section").length == 0){
				$("#hiddenPanel").hide();
			}
		});
	});
	
	$('#showsearch').bind('input propertychange', function () {
		refresh();
	});

	$('#deletesearch').on('click', function () {
		$('#showsearch').val('');
		$('#deletesearch').css('display','none');
	});
	
	//点击打开search框
	var form=document.getElementById('myLinksDivPrimary1');
	$(document).ready(function(){
		$("#showsearch").keyup(function(){ 
			form.style.display = 'block';

			/*$("#searchAreaBlock").on('dblclick',function(){
				clearTimeout(refresh);
				$('#hiddenPanel').css('display','none');
				$('#deletesearch').css('display','none');
			})*/
			/*$("#searchBlock").on('dblclick',function(){
		clearTimeout(refresh);
		$('#hiddenPanel').css('display','none');
		$('#deletesearch').css('display','none');
	})*/;
		});
	});
	
	$(document).ready(function(){
		$("#showsearch").keyup(function() {
			
			var name = $("#showsearch").val ();
			
			if (name == "") {
				$("#peopleResults").html("");
				$("#peopleEmpty").html("peopleEmpty");
				return false;
			}
			
			$.ajax({
				type: "get",
				url: "searchByname.action",
				data: {"name": name},
				success:function(data){
					var dataObject = JSON.parse(data);
					//var str = "";
					var htmlStr = " ";
					$("#peopleEmpty").html("");
					$(dataObject).each (function(index, value) {
						//str += "Name:" value.user_name + "<br/>" + "Level:" value.user_levelid + "<br/>" + "<br/>";
						htmlStr += "<img src='" + value.user_image + "' style='width:30px; height:30px; border-radius: 100px' />" + 
								   ""+value.user_name +"<br/>" + 
						           "<h style='margin-left:30px;'/>"+value.level.level_name+ "<br/>"+ "<br/>";
					});
					//htmlStr += "</table>";

					$("#peopleResults").html(htmlStr);
						if (htmlStr == " ") {
						$("#peopleEmpty").html("peopleEmpty");
					} 
				}
			});
		});
	}); 
	$(document).ready(function(){
		$("#showsearch").keyup(function() {
			
			var linkname = $("#showsearch").val ();
			
			if (linkname == "") {
				$("#siteResults").html("");
				$("#siteEmpty").html("siteEmpty");
				return false;
			}
			
			$.ajax({
				type: "get",
				url: "searchLinkByName.action",
				data: {"linkname": linkname},
				success:function(data){
					var dataObject = JSON.parse(data);
					//var str = "";
					var htmlStr = " ";
					$("#siteEmpty").html("");
					$(dataObject).each (function(index, value) {
						
						if(value.catalogueLevel == 4){
						//str += "Name:" value.user_name + "<br/>" + "Level:" value.user_levelid + "<br/>" + "<br/>";
						/*htmlStr += value.linkName + "<br/>";*/
						htmlStr += "<a href = '"+value.linkURL +"'> "+ value.linkName +"</a><br/>";
						}
					});
					//htmlStr += "</table>";

					$("#siteResults").html(htmlStr);
						if (htmlStr == " ") {
						$("#siteEmpty").html("siteEmpty");
					} 
				}
			});
		});
	});

//Announcement弹出框：
	
	/*//首页content弹窗
	$('#showArticle').on('click', function(){
		//触发content.action并且传参数article.article_id
		$('#showPopup').css('display','block');
		$('body').css('overflow','hidden');
	});*/
	/*$('#currentView').on('click', function(){
		//触发unreadList.action并且传参数number=1
		$('#currentViewPopup').css('display','block');
		$('body').css('overflow','hidden');
	});*/
	
	//二重弹窗关闭
	$('#closePopup').on('click', function(){
		//本行需要再 添加删除本页content的语句！！！
//		$('#showPopup').hide();
//		$('#showPopup').fadeOut();
		$('#showPopup').slideUp("fast");
//		$('#currentViewPopup').show();
		$('#currentViewPopup').fadeIn();
//		$('#currentViewPopup').slideDown();
	});
	//首页弹窗关闭
	$('#closePopupIndex').on('click', function(){
		//本行需要再 添加删除本页content的语句！！！
//		$('#showPopupIndex').hide();
		$('#showPopupIndex').slideUp("fast");
		$('body').css('overflow','auto');
		
		/*$('body').css('overflow','auto');*/
	});
	//分页弹窗关闭
	$('#currentClosePopup').on('click', function(){
		 $(".appendList").remove(); //删除本页列表数据
		 $("#currentPage").remove();//删除current page数据
		 
//		$('#currentViewPopup').css('display','none');
		 $('#currentViewPopup').slideUp("fast");
		$('body').css('overflow','auto');
		
		
	});
	//unreadList分页的弹框列表======================================================
		var pageNumber=1;
		var count=0;
		$("#currentView").click(function(){
			pageNumber=1;
		$.ajax({
			url:"unreadList.action?number=1",
			cache:false,
			success: function(mapMulti){
				var mapMultiObject=JSON.parse(mapMulti);
				/*console.info("mapMultiObject:"+mapMultiObject);
				console.info(typeof (mapMultiObject));*/
				count=mapMultiObject["count"];
				var dataObject=mapMultiObject["unreadList"];
				var articleList=" ";
				$.each(dataObject,function(key,value) {  
					
//					articleList += "<div class='everyArticle'><p class='artid'>"+value.article_id+"</p><div class='col-md-7'><span style='cursor:pointer;font-weight:bold;font-size:13px'>"
					articleList += "<div class='everyArticle'  style='line-height:3;border-top: 1px solid #ddd;'><div class='col-md-7'><p class='artid'>"+value.article_id+"</p><div style='letter-spacing: 0px;overflow:hidden;white-space:nowrap;cursor:pointer;font-weight:bold;font-size:13px'>"
				         + value.subject + "</div></div><div class='col-md-3'><span style='white-space:nowrap;overflow:hidden;font-weight:normal;font-size:11px'>"
				         + value.sender + "</span></div><div class='col-md-2' ><span style='white-space:nowrap;overflow:hidden;font-weight:normal;font-size:11px'>"
				         + value.time + "</span></div></div><hr>";		
				});  
					 
//			         $("#tableTitle").after(trs);
				    $("#currentArticleList").empty();
				     $("#currentArticleList").append(articleList);
			         $('.artid').hide();
			         $("#nextPage").after("<font id='currentPage'>current page" +pageNumber+ "/" +count+ "</font>")
				}
		});
//		$('#currentViewPopup').css('display','block');
		$('#currentViewPopup').slideDown("fast");
//		$('body').css('overflow','hidden');
		});
		
		//下一页按钮=======================================下一页==================================
		$("#nextPage").click(function(){
			 if(pageNumber<count){
				 pageNumber=++pageNumber;
			 }
			$.ajax({
				url:"unreadList.action?number="+pageNumber,
				cache:false,
				success: function(mapMulti){
					var mapMultiObject=JSON.parse(mapMulti);
					count=mapMultiObject["count"];
					var dataObject=mapMultiObject["unreadList"];
					var articleList=" ";
					$.each(dataObject,function(key,value) {  
						
						articleList += "<div class='everyArticle'  style='line-height:3;border-top: 1px solid #ddd;'><div class='col-md-7'><p class='artid'>"+value.article_id+"</p><div style='letter-spacing: 0px;overflow:hidden;white-space:nowrap;cursor:pointer;font-weight:bold;font-size:13px'>"
				         + value.subject + "</div></div><div class='col-md-3'><span style='white-space:nowrap;overflow:hidden;font-weight:normal;font-size:11px'>"
				         + value.sender + "</span></div><div class='col-md-2' ><span style='white-space:nowrap;overflow:hidden;font-weight:normal;font-size:11px'>"
				         + value.time + "</span></div></div><hr>";		
					});  
						 
//				         $("#tableTitle").after(trs);
					    $("#currentArticleList").empty();
					     $("#currentArticleList").append(articleList);
				         $('.artid').hide();
				         $("#currentPage").remove();
				         $("#nextPage").after("<font id='currentPage'>current page" +pageNumber+ "/" +count+ "</font>")
					}
			});
//			$('#currentViewPopup').css('display','block');
//			$('body').css('overflow','hidden');
			});
		
		//上一页按钮====================================上一页=====================================
		$("#lastPage").click(function(){
			 if(pageNumber>1){
				 pageNumber=--pageNumber;
			 }
			$.ajax({
				url:"unreadList.action?number="+pageNumber,
				cache:false,
				success: function(mapMulti){
					var mapMultiObject=JSON.parse(mapMulti);
					count=mapMultiObject["count"];
					var dataObject=mapMultiObject["unreadList"];
					var articleList=" ";
					$.each(dataObject,function(key,value) {  
						
						/*articleList += "<div class='everyArticle'><p class='artid'>"+value.article_id+"</p><div class='col-md-7'><span style='font-weight:bold;font-size:13px'>"*/
						
						articleList += "<div class='everyArticle'  style='line-height:3;border-top: 1px solid #ddd;'><div class='col-md-7'><p class='artid'>"+value.article_id+"</p><div style='letter-spacing: 0px;overflow:hidden;white-space:nowrap;cursor:pointer;font-weight:bold;font-size:13px'>"
				         + value.subject + "</div></div><div class='col-md-3'><span style='white-space:nowrap;overflow:hidden;font-weight:normal;font-size:11px'>"
				         + value.sender + "</span></div><div class='col-md-2' ><span style='white-space:nowrap;overflow:hidden;font-weight:normal;font-size:11px'>"
				         + value.time + "</span></div></div><hr>";		
					});  
						 
//				         $("#tableTitle").after(trs);
					    $("#currentArticleList").empty();
					     $("#currentArticleList").append(articleList);
				         $('.artid').hide();
				         $("#currentPage").remove();
				         $("#nextPage").after("<font id='currentPage'>current page" +pageNumber+ "/" +count+ "</font>")
					}
			});
//			$('#currentViewPopup').css('display','block');
//			$('body').css('overflow','hidden');
			
			});
		
		//分页列表content弹窗=========================================================================
		/*$("#currentArticleList").on("click",".everyArticle",function(){*/
		$("#currentArticleList").on("click",".col-md-7",function(){
//			.on("click",".appendList",
			
			
			 $("#articleContent").empty();
			 $("#pageArticleTitle").empty();
			
//			alert("dianwo");
//			$("tbody").delegate(".appendList","click",function(){
			var artid=$(this).children(".artid").text();
//			 var artid=$('.artid');
			
//			console.info(typeof(artid));
//			artid=Number(artid);
			artid=parseInt(artid,10);
			console.info(typeof(artid));
			console.info(artid);
			$.ajax({
				url:"content.action?article_id="+artid,
				/*cache:false,*/
				/*async:false,*/
				success: function(responseData){
//					console.info("content:"+content);
//					console.info(typeof (content));
//					$("#articleContent").text("");
//					$("#articleContent").text(content);
					
					var dataObject = JSON.parse(responseData);
					var con = "";
					 var title = "";
					 
					 con += "<font style='color: green'>"+dataObject.content +"</font>";
					 title += "<h3 class='modal-title'>"+ dataObject.subject+"</h3>";
					 
					
					 
					 $("#articleContent").append(con);
					 $("#pageArticleTitle").append(title);	
					}
			});
//			$('#currentViewPopup').hide();
//			$('#currentViewPopup').css('overflow','hidden');
//			$('#showPopup').show();
			$('#showPopup').slideDown("fast");
		});
		
		//首页content弹窗
		$("#getArticle").on('click',".article", function(){
			//触发content.action并且传参数article.article_id
//			var artid=$(this).children(".artid").text();
//			console.info(typeof(artid));
//			console.info(artid);
//			alert("dianwo");
			
			 $("#articleContentIndex").empty();
			 $("#articleTitle").empty();
			
			var artid=$(this).children(".artid").text();
			console.info(typeof(artid));
			console.info("before transform:"+artid);
			
			artid=parseInt(artid,10);
			console.info(typeof(artid));
			console.info("after transform:"+artid);
			
			$.ajax({
				url:"content.action?article_id="+artid,
				/*cache : false,*/
				/*async:false,*/
				success: function(responseData){
//					console.info("content:"+content);
//					console.info(typeof (content));
//					$("#articleContent").text("");
//					$("#articleContentIndex").text(content);
					
					var dataObject = JSON.parse(responseData);
					
					 var con = "";
					 var title = "";
					 /*con += "<font style='color: green'>"+dataObject[0].content+"</font>";
					 title += "<h3 class='modal-title'>"+ dataObject[0].subject+"</h3>";*/
					 con += "<font style='color: green'>"+dataObject.content+"</font>";
					 title += "<h3 class='modal-title'>"+ dataObject.subject+"</h3>";
					 
					
					 
					 $("#articleContentIndex").append(con);
					 $("#articleTitle").append(title);
					
					
					}
			});
			
//			$('#showPopupIndex').css('display','block');
			$('#showPopupIndex').slideDown("fast");
//			$('body').css('overflow','hidden');
		});
		
		
		//首页显示3条未读================================================================
		var numI = 3;
	      
	      $(document).ready(function(){
	        $.ajax({url : "readMore.action?numI=3",
	        cache:false,
	        success : function(responseData) {
	         var dataObject = JSON.parse(responseData);
	         var article = " ";
	                                                      
	         $.each(dataObject,function(key, value) {
	        	 var aId = value.article_id;
	         article += "<div class='row annoucements-item'><div class='col-md-12'><div class='row'><div class='col col-md-2'><input type='checkbox' class='select-operation' value="+aId+" name='checkMark'></div><div class='col col-md-8 article'><p class='artid'>"+value.article_id+"</p><div class='annoucements-subject'>"
	        	 		+ value.subject
	        	 		+ "</div><p class='annoucements-from'>"
	        	 		+ value.sender
	        	 		+ "</p>"
	        	 		+"</div></div><div class='row'><div class='col col-md-12 annoucements-date'>"
	        	 		+ value.time
	        	 		+ "</div></div></div></div>";
	             });
	           $("#getArticle").append(article);
	           $('.artid').hide();
	        	}
	        	});
	      		});
	      
	      //Readmore按钮
	      $("#readMoreButton").on("click",function(){ 
	    	  numI = numI + 3;
	          /* $('#articleBody').load("readMore.action"); */
	          $.ajax({
	          url : "readMore.action?numI=" + numI,
	          cache:false,
	          success : function(responseData) {
	          var dataObject = JSON.parse(responseData);
	          var moreArticle = " ";
	          
	          $.each(dataObject,function(key, value) {
	        	  var aId = value.article_id;
	          moreArticle += "<div class='row annoucements-item'><div class='col-md-12'><div class='row'><div class='col col-md-2'><input type='checkbox' class='select-operation' value="+aId+" name='checkMark'></div><div class='col col-md-8 article'><p class='artid'>"+value.article_id+"</p><div class='annoucements-subject'>"
  	 		         		+ value.subject
  	 		         		+ "</div><p class='annoucements-from'>"
  	 		         		+ value.sender
  	 		         		+ "</p>"
  	 		         		+"</div></div><div class='row'><div class='col col-md-12 annoucements-date'>"
  	 		         		+ value.time
  	 		         		+ "</div></div></div></div>";
	           	});
	           $("#getArticle").empty();
	           $("#getArticle").append(moreArticle);
	           $('.artid').hide();
	            	}
	           });
	           });
	      
	      
//		     markAsRead功能 
	      var idStr="";
	      $("#prMarkAsRead").click(
	    	function(){
	    		
	    		/*var obj=document.getElementsByName('checkMark');
	    		for(var len=0; len<obj.length; len++){
	    			if(obj[len].checked){
	    				idStr+=obj[len].value+',';
	    			}
	    		}*/
	    		
	    		$("input:checked").each(function(){
	    		idStr +=$(this).val()+",";
	    		});
	    	
	    		
	    		$.ajax({
	    			url : "markMulti.action?idStr=" + idStr,
//	    			 async:false,
	    			success:function(){
	    				
	    	
	    		 $.ajax({
                     url : "readMore.action?numI=" + numI,
                     cache : false,
                     success : function(responseData) {

                           var dataObject = JSON.parse(responseData);
                           var moreArticle = " ";
                     
                           $.each(dataObject,function(key, value) {
                        	   var aId = value.article_id;
                                 moreArticle += "<div class='row annoucements-item'><div class='col-md-12'><div class='row'><div class='col col-md-2'><input type='checkbox' class='select-operation' value="+aId+" name='checkMark'></div><div class='col col-md-8 article'><div class='annoucements-subject'>"+"<p class='artid'>"+value.article_id+"</p>"
         	        	 		+ value.subject
        	        	 		+ "</div><p class='annoucements-from'>"
        	        	 		+ value.sender
        	        	 		+ "</p>"
        	        	 		+"</div></div><div class='row'><div class='col col-md-12 annoucements-date'>"
        	        	 		+ value.time
        	        	 		+ "</div></div></div></div>";
 
                                                   });
                           $("#getArticle").empty();
                     
                           $("#getArticle").append(moreArticle);
                           $('.artid').hide();
                     }
               });
	    			}
	    	    });
	    		
	    	}	  
	      )
	      
	      
})